package edu.tgc.service.file;

import java.io.IOException;
import java.util.Collection;
import java.util.UUID;

import org.abyss.common.Constants;
import org.abyss.common.annotation.Log;
import org.abyss.common.exception.CreateFaildServiceException;
import org.abyss.common.service.CommonCrudService;
import org.abyss.upload.resolver.DefaultUploadResolverFactory;
import org.abyss.upload.resolver.UploadResolverFactory;
import org.abyss.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.tgc.entity.file.FileDoc;
import edu.tgc.entity.file.FilePath;
import edu.tgc.repository.file.FileDocRepository;

@Log(value = "文档")
@Service
public class FileDocService extends CommonCrudService<FileDoc, UUID> {

    @Autowired
    protected FileDocRepository fileDocRepository;

    @Transactional
    @Log(value = Constants.Log.CREATE)
    @Override
    public FileDoc create(FileDoc model, Collection<String> ignorePropertiesArray) {
        FileDoc entity = assertNew(model);
        try {
            preCreate(model, entity);
            BeanCopyUtils.copyPropertiesIgnore(model, entity, ignorePropertiesArray);
            // 创建流程
            entity = baseRepository.save(entity);
            baseRepository.flush();
            afterCreate(model, entity);
            return entity;
        } catch (Throwable e) {
            throw new CreateFaildServiceException(title + Constants.Log.CREATE + "失败", e);
        }
    }

    public FilePath getPath() {
        return new FilePath("c:\\file\\doc");
    }

    protected UploadResolverFactory uploadResolverFactory = new DefaultUploadResolverFactory();

    public void deleteFile(UUID fileId) throws IOException {
        this.uploadResolverFactory.build(getPath()).delete(fileId.toString(), getPath());
    }


}
